# -*- coding: utf-8 -*-
# @时间      : 2025/4/9 下午3:19
# @作者      : 高级架构师
#导入等待时间
from time import sleep
#导入启动包
from selenium import webdriver
#导入chrome服务(车)
from selenium.webdriver.chrome.service import Service
#导入一个定位元素工具 By (方向盘)
from selenium.webdriver.common.by import By
#导入一个等待元素出现的工具 WebDriverWait (等待时间)
from selenium.webdriver.support.wait import WebDriverWait
#导入一个元素定位器
from selenium.webdriver.support import expected_conditions as EC
# Expected Conditions（等待的条件，比如元素出现、可点击、可见等）


#车里装进去一个司机
service = Service(executable_path='chromedriver.exe')
#司机发动Chrome这个车
driver = webdriver.Chrome(service=service)
wait = WebDriverWait(driver, 10,0.5)  # 最长等10秒，每隔0.5秒检查一次
driver.get("https://deyunce:828123@mall.deyunce.com/pc/")
#百度全屏
driver.maximize_window()
sleep(5)
"""
我需要做的事情
"""
#进行登录操作

#进行登录操作
#等待我看到登录按键
#等页面上某个元素可见（显示在用户眼前）
wait.until(EC.visibility_of_element_located((By.XPATH,"//a[@href=\"/pc/account/login\"]")))
ele1 = driver.find_element(By.XPATH,"//a[@href=\"/pc/account/login\"]")  #第二步不用动 上面一步看到了 定位过来
wait.until(EC.element_to_be_clickable(ele1)) #判断一下是不是能点
ele1.click()  #如果可以点再

#切换成账号密码登录
wait.until(EC.visibility_of_element_located((By.XPATH,"//div[contains(text(),'账号密码登录')]")))
ele2 = driver.find_element(By.XPATH,"//div[contains(text(),'账号密码登录')]")
wait.until(EC.element_to_be_clickable(ele2))
ele2.click()

# #输入手机号
wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@placeholder="请输入账号/手机号码"]')))
ele3 = driver.find_element(By.XPATH,'//input[@placeholder="请输入账号/手机号码"]')
wait.until(EC.visibility_of(ele3))  #我可以给ele3 输入文字
ele3.send_keys("15559570963")

#输入密码
wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@placeholder="请输入密码"]')))
ele4 = driver.find_element(By.XPATH,'//input[@placeholder="请输入密码"]')
wait.until(EC.visibility_of(ele3))  #我可以给ele3 输入文字
ele4.send_keys("dyc123123")


# 点击登录按钮
wait.until(EC.visibility_of_any_elements_located((By.XPATH,'//span[contains(text(),\'立即登录\')]')))
ele5 = driver.find_elements(By.XPATH,'//span[contains(text(),\'立即登录\')]')[1]
wait.until(EC.element_to_be_clickable(ele5))
ele5.click()


#商品搜索 输入熊猫
wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@placeholder="请输入要搜索的商品名称"]')))
ele6 = driver.find_element(By.XPATH,'//input[@placeholder="请输入要搜索的商品名称"]')
ele6.send_keys("熊猫")
wait.until(EC.visibility_of(ele6))  #我可以给ele3 输入文字
ele6.click()


#点击搜索按键
wait.until(EC.visibility_of_element_located((By.XPATH,'//div[contains(text(),\"搜索\")]')))
ele7 = driver.find_element(By.XPATH,"//div[contains(text(),\"搜索\")]")
wait.until(EC.visibility_of(ele7))  #我可以给ele3 输入文字
ele7.click()


#选择 一只公熊猫点击
wait.until(EC.visibility_of_element_located((By.XPATH,"//div[contains(text(),'野生黑熊猫前腿肉 滋阴补肾 壮阳增胆')]")))
ele8 = driver.find_element(By.XPATH,"//div[contains(text(),'野生黑熊猫前腿肉 滋阴补肾 壮阳增胆')]")
wait.until(EC.element_to_be_clickable(ele8))
ele8.click()

#点击立即购买
wait.until(EC.visibility_of_element_located((By.XPATH,'//div[text()="立即购买"]')))
ele9 = driver.find_element(By.XPATH,'//div[text()="立即购买"]')
wait.until(EC.element_to_be_clickable(ele9))
ele9.click()

#处理alert 加上 智能等待
wait.until(EC.alert_is_present())
driver.switch_to.alert.accept()

#点击提交订单
wait.until(EC.visibility_of_element_located((By.XPATH,'//div[contains(text(),"提交订单")]')))
ele10 = driver.find_element(By.XPATH,'//div[contains(text(),"提交订单")]')
wait.until(EC.element_to_be_clickable(ele10))
ele10.click()

#点击账户余额
wait.until(EC.visibility_of_element_located((By.XPATH,'//span[text()="账户余额"]')))
ele11 = driver.find_element(By.XPATH,'//span[text()="账户余额"]')
wait.until(EC.element_to_be_clickable(ele11))
ele11.click()

sleep(6)
#可以不写 关闭 浏览器
driver.quit()